package in.muton.edushare.pages.common;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.autocomplete.EdushareTextArea;
import in.muton.edushare.newentities.CourseUserCreator;
import in.muton.edushare.newentities.DeptUserCreator;
import in.muton.edushare.newentities.GroupUserCreator;
import in.muton.edushare.newentities.UnivUserCreator;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;

public class Invite extends EdusharePanel{

	private static final long serialVersionUID = -6471994822672420414L;

	public Invite(String id, IModel<?> obj, EduShareBasePage page) {
		super(id, obj, page);
		gr = null;
		getMap();
		status = new StatusMessages();
		add(CSSPackageResource
				.getHeaderContribution(Invite.class, "Invite.css"));
		
		WebMarkupContainer wmc = new WebMarkupContainer("msgContainer"){
			private static final long serialVersionUID = 2733664685461273985L;

			@Override
			public boolean isVisible(){
				return (status.str.size()>0);
			}
		};
		wmc.add(new ListView<String>("message", status) {
			private static final long serialVersionUID = -220615465169149296L;

			@Override
			protected void populateItem(ListItem<String> arg0) {
				arg0.add(new Label("string",arg0.getModelObject()));
			}
		});
		add(wmc);
		
		Form<Void> f = new Form<Void>("invite") {
			private static final long serialVersionUID = -3882006121926947925L;

			@Override
			public void onSubmit() {
				if(!StringUtil.isValid(getUserList())){
					setError("Please provide email IDs/Edushare IDs to invite users.");
					return;
				}
						
				addUsers();
				setUserList("");
			}
		};
		add(f);
		f.add(new EdushareTextArea("userList", new PropertyModel<String>(this,
				"userList")));
		f.add(new DropDownChoice<String>("roleSelect", gr.getModel(), gr
				.getOptions(), gr));
	}

	private String userList = "";

	public void setUserList(String userList) {
		this.userList = userList;
	}

	public String getUserList() {
		return userList;
	}

	public class StatusMessages implements IModel<List<String>> {
		private static final long serialVersionUID = -8431934505232025750L;
		private List<String> str = new ArrayList<String>();

		public List<String> getObject() {
			return str;
		}

		public void setObject(List<String> arg0) {
			str = arg0;
		}

		public void detach() {
		}
	}

	private void getMap() {
		if (getDefaultModelObject() instanceof Universities) {
			gr = new GenericRenderer<Integer>(univMap, UserRoles.STUDENT_ROLE);
		}
		if (getDefaultModelObject() instanceof Departments) {
			gr = new GenericRenderer<Integer>(deptMap, UserDeptLink.STUDENT);
		}
		if (getDefaultModelObject() instanceof CourseInstances) {
			gr = new GenericRenderer<Integer>(courseMap, Enrolments.STUDENT);
		}
		if (getDefaultModelObject() instanceof Groups) {
			gr = new GenericRenderer<Integer>(groupMap, GroupMembers.GROUP_MEMBER);
		}
	}

	private void addUsers() {
		ORMInterface orm = EduShareSession.get().getSessionORMInterface();
		if (getDefaultModelObject() instanceof Universities) {
			IModel<Universities> m = new CayenneModel<Universities>(
					(Universities) getDefaultModelObject());
			status.setObject(new UnivUserCreator(orm, m, gr.getSelection())
					.addUsersToUniv(userList));
		}
		if (getDefaultModelObject() instanceof Departments) {
			IModel<Departments> m = new CayenneModel<Departments>(
					(Departments) getDefaultModelObject());
			status.setObject(new DeptUserCreator(orm, m, gr.getSelection())
					.addUsersToDept(userList));
		}
		if (getDefaultModelObject() instanceof CourseInstances) {
			IModel<CourseInstances> m = new CayenneModel<CourseInstances>(
					(CourseInstances) getDefaultModelObject());
			status.setObject(new CourseUserCreator(orm, m, gr.getSelection())
					.addUsersToCourse(userList));
		}
		if (getDefaultModelObject() instanceof Groups) {
			IModel<Groups> m = new CayenneModel<Groups>((Groups) getDefaultModelObject());
			status.setObject(new GroupUserCreator(orm, m, gr.getSelection()).addUsers(userList));
		}
	}

	private StatusMessages status = null;

	private final static HashMap<String, Integer> univMap = new HashMap<String, Integer>();
	private final static HashMap<String, Integer> deptMap = new HashMap<String, Integer>();
	private final static HashMap<String, Integer> courseMap = new HashMap<String, Integer>();
	private final static HashMap<String, Integer> groupMap = new HashMap<String, Integer>();
	
	private GenericRenderer<Integer> gr;
	static {
		univMap.put("Administrator", UserRoles.ADMIN_ROLE);
		univMap.put("Faculty", UserRoles.TEACHER_ROLE);
		univMap.put("Student", UserRoles.STUDENT_ROLE);

		deptMap.put("Administrator", UserDeptLink.PRIMARY_ADMIN);
		deptMap.put("Faculty", UserDeptLink.TEACHER);
		deptMap.put("Student", UserDeptLink.STUDENT);

		courseMap.put("Faculty", Enrolments.TEACHER);
		courseMap.put("Teaching Assistant", Enrolments.TEACHING_ASSISTANT);
		courseMap.put("Student", Enrolments.STUDENT);
		courseMap.put("Audit", Enrolments.OBSERVER);
		
		groupMap.put("Administrator", GroupMembers.GROUP_ADMIN);
		groupMap.put("Member", GroupMembers.GROUP_MEMBER);
	}
}
